package test;

/**
 * This example code demonstrates how to perform simple state control of a GPIO
 * pin on the Raspberry Pi.
 *
 * @author Robert Savage
 */
public class ControlGpioExample {
	public static void main(String[] args) throws InterruptedException {
		// 创建一个GPIO控制器
		final GpioController gpio = GpioFactory.getInstance();

		// 获取1号GPIO针脚并设置高电平状态，对应的是树莓派上的12号针脚，可以参考pi4j提供的图片。
		final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "LED", PinState.HIGH);

		while (true) {
			// 设置高电平
			pin.high();
			System.out.println("打开继电器");
			// 睡眠1秒
			Thread.sleep(1000);
			// 设置低电平
			pin.low();
			System.out.println("关闭继电器");
			Thread.sleep(1000);
			// 切换状态
			// pin.toggle();
		}
	}

	// public static void main(String[] args) throws InterruptedException {
	//
	// System.out.println("<--Pi4J--> GPIO Control Example ... started.");
	//
	// // create gpio controller
	// final GpioController gpio = GpioFactory.getInstance();
	//
	// // provision gpio pin #01 as an output pin and turn on
	// final GpioPinDigitalOutput pin =
	// gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
	//
	// // set shutdown state for this pin
	// pin.setShutdownOptions(true, PinState.LOW);
	//
	// System.out.println("--> GPIO state should be: ON");
	//
	// Thread.sleep(5000);
	//
	// // turn off gpio pin #01
	// pin.low();
	// System.out.println("--> GPIO state should be: OFF");
	//
	// Thread.sleep(5000);
	//
	// // toggle the current state of gpio pin #01 (should turn on)
	// pin.toggle();
	// System.out.println("--> GPIO state should be: ON");
	//
	// Thread.sleep(5000);
	//
	// // toggle the current state of gpio pin #01 (should turn off)
	// pin.toggle();
	// System.out.println("--> GPIO state should be: OFF");
	//
	// Thread.sleep(5000);
	//
	// // turn on gpio pin #01 for 1 second and then off
	// System.out.println("--> GPIO state should be: ON for only 1 second");
	// pin.pulse(1000, true); // set second argument to 'true' use a blocking
	// call
	//
	// // stop all GPIO activity/threads by shutting down the GPIO controller
	// // (this method will forcefully shutdown all GPIO monitoring threads and
	// scheduled tasks)
	// gpio.shutdown();
	//
	// System.out.println("Exiting ControlGpioExample");
	// }
}
